<template>
	<div>
		登录
		<input type="text" v-model="user">
		<input type="text" v-model="pwd">
		<button @click="handleLogin">登录</button>
	</div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const user = ref('')
const pwd = ref('')
const router = useRouter()

const handleLogin = async () => {
	const resp = await axios.post('/api/signin', {
		user: user.value,
		pwd: pwd.value
	})
	const { data, message, code } = resp.data;

	if (code === 200) {
		ElMessage.success(message);
		localStorage.setItem('token', data.token);
		router.push('/')
	} else {
		ElMessage.error(message);
	}
}

</script>